﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB;
using YJKCodeBase.XmlUtility;

namespace ModelChecking.ModelValidation.ClearHeightCheck
{
	public partial class CategoryTypeSelForm : System.Windows.Forms.Form
	{
		public CategoryTypeSelForm(SelectCategrayType selectCategrayType, bool bIsCheck = false)
		{
			this.InitializeComponent();
			this.m_bIsCheck = bIsCheck;
			//base.Icon = Resource.YArchitechIcon;
			this.m_selectCategrayType = selectCategrayType;
			this.xmlHelper.LoadXml(this.path, "Root");
		}

		private void initTreeView()
		{
			this.treeViewSelType.Items.Clear();
			this.treeViewSelType.LabelEdit = false;
			List<CategoryItem> list = new List<CategoryItem>();
			List<CategoryItem> categoryItemListFromXML = SelectCategrayType.getCategoryItemListFromXML(this.m_bIsCheck);
			List<CategoryItem> categoryList = this.m_selectCategrayType.getCategoryList();
			if (this.IsChangeCategoryItems(categoryItemListFromXML, categoryList))
			{
				list = categoryList;
			}
			else
			{
				list = categoryItemListFromXML;
			}
			foreach (CategoryItem categoryItem in list)
			{
				TreeListViewItem treeListViewItem = new TreeListViewItem(categoryItem.CategoryName(), 0);
				treeListViewItem.Checked = categoryItem.Selected;
				treeListViewItem.Expand();
				treeListViewItem.Tag = categoryItem;
				this.treeViewSelType.Items.Add(treeListViewItem);
				this.initObjectNode(treeListViewItem, categoryItem.ObjectTypeItemList);
			}
		}

		private bool IsChangeCategoryItems(List<CategoryItem> oneItems, List<CategoryItem> twoItems)
		{
			if (oneItems.Count != twoItems.Count)
			{
				return true;
			}
			for (int i = 0; i < oneItems.Count; i++)
			{
				if (oneItems[i].CategoryId() != twoItems[i].CategoryId() || !(oneItems[i].CategoryName() == twoItems[i].CategoryName()))
				{
					return false;
				}
				if (this.IsChangeObjectItems(oneItems[i].ObjectTypeItemList, twoItems[i].ObjectTypeItemList))
				{
					return true;
				}
			}
			return false;
		}

		private bool IsChangeObjectItems(List<ObjectTypeItem> oneItems, List<ObjectTypeItem> twoItems)
		{
			if (oneItems.Count != twoItems.Count)
			{
				return true;
			}
			for (int i = 0; i < oneItems.Count; i++)
			{
				if (oneItems[i].getTypeName() != twoItems[i].getTypeName())
				{
					return true;
				}
			}
			return false;
		}

		private void initObjectNode(TreeListViewItem rootNode, List<ObjectTypeItem> objectTypeItemList)
		{
			foreach (ObjectTypeItem objectTypeItem in objectTypeItemList)
			{
				TreeListViewItem treeListViewItem = new TreeListViewItem(objectTypeItem.getTypeName(), 0);
				treeListViewItem.Checked = objectTypeItem.Selected;
				treeListViewItem.Tag = objectTypeItem;
				rootNode.Items.Add(treeListViewItem);
			}
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			foreach (object obj in this.treeViewSelType.Items)
			{
				TreeListViewItem treeListViewItem = (TreeListViewItem)obj;
				treeListViewItem.Checked = true;
			}
		}

		private void btnSelNone_Click(object sender, EventArgs e)
		{
			foreach (object obj in this.treeViewSelType.Items)
			{
				TreeListViewItem treeListViewItem = (TreeListViewItem)obj;
				treeListViewItem.Checked = false;
			}
		}

		private void btnExpandAll_Click(object sender, EventArgs e)
		{
			this.treeViewSelType.ExpandAll();
		}

		private void btnCollapseAll_Click(object sender, EventArgs e)
		{
			this.treeViewSelType.CollapseAll();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			List<CategoryItem> list = new List<CategoryItem>();
			foreach (object obj in this.treeViewSelType.Items)
			{
				TreeListViewItem treeListViewItem = (TreeListViewItem)obj;
				object obj2 = null;
				obj2 = treeListViewItem.Tag;
				if (obj2 is CategoryItem)
				{
					CategoryItem categoryItem = obj2 as CategoryItem;
					if (treeListViewItem.Checked)
					{
						categoryItem.SetSelected(true);
					}
					else
					{
						categoryItem.SetSelected(false);
					}
					foreach (object obj3 in treeListViewItem.Items)
					{
						TreeListViewItem treeListViewItem2 = (TreeListViewItem)obj3;
						ObjectTypeItem objectTypeItem = treeListViewItem2.Tag as ObjectTypeItem;
						if (objectTypeItem != null)
						{
							if (treeListViewItem2.Checked)
							{
								objectTypeItem.SetSelected(true);
							}
							else
							{
								objectTypeItem.SetSelected(false);
							}
						}
					}
				}
				if (obj2 != null)
				{
					list.Add(obj2 as CategoryItem);
				}
			}
			this.m_selectCategrayType.m_CategoryItemList = list;
			this.m_selectCategrayType.SaveToXmlFile(this.m_bIsCheck);
			this.SaveDlgData();
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.SaveDlgData();
			base.Close();
		}

		private void CategoryTypeSelForm_Load(object sender, EventArgs e)
		{
			this.initTreeView();
			this.LoadDlgData();
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "CategoryTypeSelForm"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "CategoryTypeSelForm", "Params"));
			this.xmlHelper.SetAttribute("X", base.Location.X.ToString());
			this.xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "CategoryTypeSelForm", "Params"));
			string attribute = this.xmlHelper.GetAttribute("X");
			string attribute2 = this.xmlHelper.GetAttribute("Y");
			if (!string.IsNullOrEmpty(attribute) && !string.IsNullOrEmpty(attribute2))
			{
				base.Location = new Point(Convert.ToInt32(attribute), Convert.ToInt32(attribute2));
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.Close();
			}
			return false;
		}

		private const string Root = "Root";

		private const string Node = "CategoryTypeSelForm";

		private const string ChildNode = "Params";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private List<CategoryItem> categoryItemList = new List<CategoryItem>();

		private SelectCategrayType m_selectCategrayType;

		private YJKCodeBase.XmlUtility.XmlHelper xmlHelper = new YJKCodeBase.XmlUtility.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CategoryTypeSelForm.xml");

		private bool m_bIsCheck;
	}
}
